﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Notepad.Search;
using Notepad.About;
using Notepad.GoTo;

namespace Notepad
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        ///创立一个用于存储文件名的属性
        public string fileName;

        ///新建
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///清除栈
            Content.Clear();
            textboxMainContent.Text = "";
            fileName = "";

        }


        /// 打开          
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///清除栈
            Content.Clear();

            ///读取的文本内容
            string FileContent;

            ///建立对象
            OpenFileDialog ofd = new OpenFileDialog();

            ///标题
            ofd.Title = "打开文本文件";

            //过滤不同类型的文件
            ofd.Filter = "文本文件(*.txt) | *.txt";

            ///显示文本对话框
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                textboxMainContent.Text = File.ReadAllText(ofd.FileName);

                ///保存文件名
                fileName = ofd.FileName;

            }
        }

        /// 保存
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///如果文件名不存在，调用“另存为”程序
            if (fileName == null || fileName.Length == 0)
            {
                ///建立对象
                SaveFileDialog sfd = new SaveFileDialog();

                ///标题
                sfd.Title = "另存为";

                //过滤不同类型的文件
                sfd.Filter = "文本文件(*.txt) | *.txt";

                ///显示文本对话框
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    ///获取存储文件的路径
                    string path = sfd.FileName;

                    ///创立文件并输入内容
                    File.WriteAllText(path, textboxMainContent.Text);


                }
            }
            ///已存在文件
            else
            {
                File.WriteAllText(fileName, textboxMainContent.Text);
            }


        }


        //另存为
        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///建立对象
            SaveFileDialog sfd = new SaveFileDialog();

            ///标题
            sfd.Title = "另存为";

            //过滤不同类型的文件
            sfd.Filter = "文本文件(*.txt) | *.txt";

            ///显示文本对话框
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                ///获取存储文件的路径
                string path = sfd.FileName;

                ///创立文件并输入内容
                File.WriteAllText(path, textboxMainContent.Text);


            }
        }

        ///创立栈用于存储数据
        Stack<string> Content = new Stack<string>();

        ///页面设置
        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.ShowDialog();
        }

        ///打印
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                ///将打印设置传给打印文档
                printDocument1.PrinterSettings = printDialog1.PrinterSettings;

                ///将页面设置传给打印文档
                printDocument1.DefaultPageSettings = pageSetupDialog1.PageSettings;

                ///开始打印
                printDocument1.Print();
            }
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///将当前的文本出栈
            if (Content.Count > 0)
                Content.Pop();

            ///此时栈顶是上一次输入的文本，出栈后内容改变，再一次入栈，栈顶为上次输入文本
            if (Content.Count > 0)
                textboxMainContent.Text = Content.Pop();
            ///当只有一个元素时会使此时的Count为零
            else if (Content.Count == 0)
            {
                textboxMainContent.Text = null;
                ///撤销禁用
                撤销ToolStripMenuItem.Enabled = false;
            }
        }

        ///文本框       
        private void textboxMainContent_TextChanged(object sender, EventArgs e)
        {
            ///入栈
            Content.Push(textboxMainContent.Text);

            ///当栈中元素数量大于0时启用撤销、查找功能,否之禁用
            撤销ToolStripMenuItem.Enabled = Content.Count > 0;

            查找ToolStripMenuItem.Enabled = Content.Count > 0;

            查找下一个ToolStripMenuItem.Enabled = Content.Count > 0;

            查找上一个ToolStripMenuItem.Enabled = Content.Count > 0;

            ///更改状态栏数据，避免输入回车时更新不及时

            ///光标左边的字符串
            string strLeft = textboxMainContent.Text.Substring(0, textboxMainContent.SelectionStart);

            ///最后一个回车出现的位置
            int lastIndex = strLeft.LastIndexOf('\n');

            ///光标所在列
            int intCol = textboxMainContent.SelectionStart - lastIndex;

            ///计算回车数
            string[] arrayString = strLeft.Split('\n');

            ///计算行数
            int intRow = arrayString.Length;

            ///显示行数与列数
            toolStripStatusLabel1.Text = $"第{intRow}行，第{intCol}列";

        }


        ///检查是否打开剪切、粘贴、复制、查找功能  
        private void Check()
        {
            ///如果有选择的文本，则打开剪切、复制、删除功能
            if (textboxMainContent.SelectedText != "")
            {
                剪切ToolStripMenuItem.Enabled = true;
                复制ToolStripMenuItem.Enabled = true;
                删除ToolStripMenuItem.Enabled = true;

            }
            else
            {
                剪切ToolStripMenuItem.Enabled = false;
                复制ToolStripMenuItem.Enabled = false;
                删除ToolStripMenuItem.Enabled = false;
            }


            ///判断粘贴功能是否打开,看剪切板是否为空
            if (Clipboard.ContainsText())
            {
                粘贴ToolStripMenuItem.Enabled = true;
            }
            else
            {
                粘贴ToolStripMenuItem.Enabled = false;
            }


            ///更改状态栏数据

            ///光标左边的字符串
            string strLeft = textboxMainContent.Text.Substring(0, textboxMainContent.SelectionStart);

            ///最后一个回车出现的位置
            int lastIndex = strLeft.LastIndexOf('\n');

            ///光标所在列
            int intCol = textboxMainContent.SelectionStart - lastIndex ;

            ///计算回车数
            string[] arrayString = strLeft.Split('\n');

            ///计算行数
            int intRow = arrayString.Length ;

            ///显示行数与列数
            toolStripStatusLabel1.Text = $"第{intRow}行，第{intCol}列";


        }

        private void textboxMainContent_MouseUp(object sender, MouseEventArgs e)
        {
            Check();
        }

        private void textboxMainContent_KeyDown(object sender, KeyEventArgs e)
        {
            Check();
        }



        ///剪切
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            textboxMainContent.Cut();

        }

        ///复制
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textboxMainContent.SelectedText);
        }

        ///粘贴

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textboxMainContent.SelectedText = Clipboard.GetText();
        }

        ///删除

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textboxMainContent.SelectedText = "";
        }

        ///查找
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSearch search = new frmSearch();
            ///传参
            search.Search += find;
            search.Show();
        }

        ///利用结构存储条件
        public struct Requirement
        {
            public string SearchText;
            public bool IsUp;
            public bool IsCaseSensitive;
        }
        ///实例化
        public Requirement requirement;

        public void find(string SearchText, bool IsUp, bool IsCaseSensitive)
        {
            ///储存条件
            requirement.SearchText = SearchText;
            requirement.IsUp = IsUp;
            requirement.IsCaseSensitive = IsCaseSensitive;

            ///开始搜索
            searchNext();
        }
        ///搜索功能
        private void searchNext()
        {
            ///光标结束位置
            int intAns = -1;

            ///利用所在位置
            int IndexNow;

            ///判断是否为向上搜索
            if (requirement.IsUp == true)
            {
                ///文本框中选定文本位置
                IndexNow = textboxMainContent.SelectionStart;

                ///表示需要搜索的文本，利用substring截取字符串
                string strSearch = textboxMainContent.Text.Substring(0, IndexNow);

                ///判断大小写是否敏感
                if (requirement.IsCaseSensitive)
                {
                    ///搜索位置
                    intAns = strSearch.LastIndexOf(requirement.SearchText);
                }
                else
                {
                    ///大小不敏感
                    intAns = strSearch.LastIndexOf(requirement.SearchText, StringComparison.CurrentCultureIgnoreCase);

                }

            }
            else
            {
                ///向下搜索
                ///文本框中选定文本位置,当勾选的是一串字符时光标位置移到尾部
                IndexNow = textboxMainContent.SelectionStart + textboxMainContent.SelectionLength;

                ///表示需要搜索的文本，利用substring截取字符串，从开始位置到结尾
                string strSearch = textboxMainContent.Text.Substring(IndexNow);

                ///判断大小写是否敏感
                if (requirement.IsCaseSensitive)
                {
                    ///搜索位置,利用Indexof寻找第一次出现位置
                    intAns = strSearch.IndexOf(requirement.SearchText);
                }
                else
                {
                    ///大小不敏感    
                    intAns = strSearch.IndexOf(requirement.SearchText, StringComparison.CurrentCultureIgnoreCase);

                }

            }
            ///找不到
            if (intAns < 0)
            {
                MessageBox.Show($"找不到{requirement.SearchText}");
            }
            else
            {
                ///显示字符串的位置

                ///激活窗口,显示搜索结果
                Activate();
                if (!requirement.IsUp)
                {
                    ///搜索文本时是在剪切了的strSearch搜索，当向下搜索时需要加上光标的首位置
                    intAns += IndexNow;
                }
                ///设置搜索结果
                textboxMainContent.SelectionStart = intAns;
                textboxMainContent.SelectionLength = requirement.SearchText.Length;

            }

        }



        ///字体
        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                ///获取字体
                textboxMainContent.Font = fontDialog1.Font;

        }
        private void 查找上一个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (requirement.SearchText != null && requirement.SearchText != "")

            {
                ///暂时存储原本方向
                bool Staging = requirement.IsUp;
                //更改查询方向
                requirement.IsUp = true;
                searchNext();

                //返还原本的方向
                requirement.IsUp = Staging;
            }

            ///如果没有开始则直接调用搜索
            else
                查找ToolStripMenuItem_Click(null, null);
        }

        ///查找下一个就是向下查找
        private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            if (requirement.SearchText != null && requirement.SearchText != "")
            {
                ///暂时存储原本方向
                bool Staging=requirement.IsUp;
                //更改查询方向
                requirement.IsUp = false;
                searchNext();
                
                //返还原本的方向
                requirement.IsUp = Staging;
            }

            ///如果没有开始则直接调用搜索
            else
                查找ToolStripMenuItem_Click(null, null);
        }



        private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 自动换行ToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            if(自动换行ToolStripMenuItem.Checked)
            {
                ///自动换行
                    textboxMainContent.WordWrap = true;
                
                ///显示纵向滚动条
                textboxMainContent.ScrollBars = ScrollBars.Vertical;
            }
            else
            {
                textboxMainContent.WordWrap = false;

                ///双向滚动条
                textboxMainContent.ScrollBars = ScrollBars.Both;

            }
        }

        private void 状态栏ToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            statusStrip1.Visible = 状态栏ToolStripMenuItem.Checked;
        }

        private void 转到ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///实例化
            frmGoto Goto = new frmGoto();
            if (Goto.ShowDialog() == DialogResult.OK)
            {
                int intLine;
                ///看是否能转化为整数
                if (int.TryParse(Goto.Tag.ToString(), out intLine))
                {
                    if (intLine > 0)
                    {
                        int intPosition = 0;
                        if (intLine > 1)
                        {
                            for (int i = 1; i < intLine; i++)
                            {
                                ///回车位置
                                int intNext = textboxMainContent.Text.IndexOf('\n', intPosition);

                                if (intNext >= 0)
                                {
                                    ///intPosition位于回车后的位置，即下一行第一个位置
                                    intPosition = intNext + 1;
                                }
                                else
                                    break;
                            }
                        }
                        if (intPosition >= 0)
                        {
                            ///光标位置
                            textboxMainContent.SelectionStart = intPosition;

                            ///内容选区为零
                            textboxMainContent.SelectionLength = 0;

                            ///更新状态栏
                            Check();
                        }
                    }
                }

            }
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textboxMainContent.SelectionStart = 0;
            textboxMainContent.SelectionLength = textboxMainContent.Text.Length;
        }

        private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ///光标选中区域
            textboxMainContent.SelectionLength = 0;
            
            ///选中区域添加时间
            textboxMainContent.SelectedText = System.DateTime.Now.ToString();

        }
    }
}
